package it.bela.pgcdc.editor;

import it.bela.pgcdc.domain.Preferenza;
import it.bela.pgcdc.service.PreferenzaService;

import java.beans.PropertyEditorSupport;

public class PreferenzaPropertyEditor extends PropertyEditorSupport {

	private PreferenzaService preferenzaService;
 
	public PreferenzaPropertyEditor(PreferenzaService preferenzaService) {
		this.preferenzaService = preferenzaService;
	}
	
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
    	Long id = null;
    	Preferenza preferenza = null;
    	if (!text.equals("-")){
    		id = new Long(text);
    		preferenza = preferenzaService.findById(Preferenza.class, id);
    	}
        super.setValue(preferenza);
    }
    
    @Override
    public String getAsText() {
    	Preferenza preferenza = (Preferenza) getValue();
    	String text = null;
    	if (preferenza != null)
    		text = preferenza.getId().toString();
    	return text;
    }
	
}
